﻿using NetCore8Demo.Models.Communication.SerialPortFolder;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NetCore8Demo.Models.PLC;
using NetCore8Demo.Models.Param;
using NetCore8Demo.Views.Param;

namespace NetCore8Demo.Views.PLC
{
    public partial class EditPlcForm : Form
    {
        public EditPlcForm()
        {
            InitializeComponent();
        }



        List<PlcInfo> _plcInfoList = new();
        int _comIndex;
        private void NewBtn_Click(object sender, EventArgs e)
        {
            List<ParamInfo> paramList = new();
            int tempIndex = 0;
            if (_plcInfoList.Count > 0)
            {
                tempIndex = _plcInfoList.Max(t => t.Index) + 1;
            }
            paramList.Add(new ParamInfo("序号", tempIndex + ""));
            paramList.Add(new ParamInfo("名称"));
            paramList.Add(new ParamInfo("IP", "192.168.0.1"));
            paramList.Add(new ParamInfo("端口号", "102"));
            ParamInfo typeParam = new ParamInfo
            {
                LabelName = "PLC类型",
                OldValue = "",
                ParamType = ParamType.ComboBox,
                ValueList = new List<string>(Enum.GetNames(typeof(PlcType))),
            };
            paramList.Add(typeParam);

            ParamInfo inUseParam = new ParamInfo
            {
                LabelName = "是否启用",
                // inUseParam.OldValue = ;
                ParamType = ParamType.YesOrNo
            };
            paramList.Add(inUseParam);



            BasicParamForm basicParamForm = new BasicParamForm(paramList);
            basicParamForm.ShowDialog();
            if (!basicParamForm.SureFlag)
            {
                return;
            }
            PlcInfo plcInfo = new PlcInfo
            {
                Id = Guid.NewGuid()
            };
            int.TryParse(paramList[0].NewValue, out plcInfo.Index);
            plcInfo.Name = paramList[1].NewValue;
            plcInfo.Ip = paramList[2].NewValue;
            int.TryParse(paramList[3].NewValue, out plcInfo.Port);
            Enum.TryParse(paramList[4].NewValue, out plcInfo.PlcType);
            bool.TryParse(paramList[5].NewValue, out plcInfo.IsInUse);
            _plcInfoList.Add(plcInfo);
            SavaPlcInfoList();
        }

        private void DelBtn_Click(object sender, EventArgs e)
        {
            if (PlcInfoListView.SelectedItems.Count == 0)
            {
                MessageBox.Show("请先选择");
                return;
            }
            _comIndex = PlcInfoListView.SelectedIndices[0];
            string id = this.PlcInfoListView.Items[_comIndex].SubItems[0].Text.Trim();

            _plcInfoList.RemoveAll(p => p.Id == Guid.Parse(id));

            SavaPlcInfoList();
        }

        private void EditPlcAddressBtn_Click(object sender, EventArgs e)
        {
            if (PlcInfoListView.SelectedItems.Count == 0)
            {
                MessageBox.Show("请先选择");
                return;
            }
            _comIndex = PlcInfoListView.SelectedIndices[0];
            string id = this.PlcInfoListView.Items[_comIndex].SubItems[0].Text.Trim();

            EditPlcAddressForm form = new EditPlcAddressForm(Guid.Parse(id));
            form.ShowDialog();
        }

        private void EditPlcForm_Load(object sender, EventArgs e)
        {
            LoadPlcInfoList();
        }


        private void PlcInfoListView_DoubleClick(object sender, EventArgs e)
        {
            _comIndex = PlcInfoListView.SelectedIndices[0];
            string id = this.PlcInfoListView.Items[_comIndex].SubItems[0].Text.Trim();
            PlcInfo? oldInfo = _plcInfoList.FirstOrDefault(p => p.Id == Guid.Parse(id));
            if (oldInfo == null) { return; }
            List<ParamInfo> paramList =
            [
                new("序号", oldInfo.Index + ""),
                new ParamInfo("名称", oldInfo.Name),
                new ("IP", oldInfo.Ip),
                new ("端口号", oldInfo.Port+"")
            ];

            ParamInfo typeParam = new ParamInfo
            {
                LabelName = "PLC类型",
                OldValue = oldInfo.PlcType.ToString(),
                ParamType = ParamType.ComboBox,
                ValueList = new List<string>(Enum.GetNames(typeof(PlcType))),
            };
            paramList.Add(typeParam);

            ParamInfo param = new ParamInfo();
            param.LabelName = "是否启用";
            param.OldValue = oldInfo.IsInUse.ToString();
            param.ParamType = ParamType.YesOrNo;
            paramList.Add(param);
            BasicParamForm basicParamForm = new BasicParamForm(paramList);
            basicParamForm.ShowDialog();
            if (!basicParamForm.SureFlag)
            {
                return;
            }

            foreach (var item in _plcInfoList)
            {
                if (item.Id == Guid.Parse(id))
                {
                    int.TryParse(paramList[0].NewValue, out item.Index);
                    item.Name = paramList[1].NewValue;
                    item.Ip = paramList[2].NewValue;
                    int.TryParse(paramList[3].NewValue, out item.Port);
                    Enum.TryParse(paramList[4].NewValue, out item.PlcType);
                    bool.TryParse(paramList[5].NewValue, out item.IsInUse);
                    break;
                }
            }
            SavaPlcInfoList();
        }

        #region 方法

        public void SavaPlcInfoList()
        {
            //升序排列
            _plcInfoList = _plcInfoList.OrderBy(s => s.Index).ToList();
            StaticPlc.SavaPlcInfoList(_plcInfoList);

            LoadPlcInfoList();
        }

        public void LoadPlcInfoList()
        {
            _plcInfoList = StaticPlc.LoadPlcInfoList();

            this.PlcInfoListView.Items.Clear();
            foreach (var item in _plcInfoList)
            {

                ListViewItem lvi = new ListViewItem
                {
                    Text = item.Id + ""
                };
                lvi.SubItems.Add(item.Index + "");
                lvi.SubItems.Add(item.Name);
                lvi.SubItems.Add(item.Ip);
                lvi.SubItems.Add(item.Port + "");
                lvi.SubItems.Add(item.PlcType + "");
                lvi.SubItems.Add(item.IsInUse ? "启用" : "停用");
                this.PlcInfoListView.Items.Add(lvi);
            }
        }

        #endregion


        private void ShowPlcBtn_Click(object sender, EventArgs e)
        {
            if (PlcInfoListView.SelectedItems.Count == 0)
            {
                MessageBox.Show("请先选择");
                return;
            }

            _comIndex = PlcInfoListView.SelectedIndices[0];
            string id = this.PlcInfoListView.Items[_comIndex].SubItems[0].Text.Trim();

            List<PlcAddressInfo> showList = StaticPlc.PlcAddressInfoList.Where(plcInfo => plcInfo.PlcInfoId == Guid.Parse(id)).ToList();
            List<string> strList = ["PLC信息", "扩展信息"];
            ShowDynPlcAddressForm form = new ShowDynPlcAddressForm(showList, strList);
            form.ShowDialog();

        }
    }
}
